#include "MemoryMonitor.h"

#ifdef _WIN32
#include <windows.h>
#include <psapi.h>

double MemoryMonitor::getCurrentMemoryUsageMB() {
    PROCESS_MEMORY_COUNTERS_EX pmc;
    HANDLE processHandle = GetCurrentProcess();

    if (GetProcessMemoryInfo(processHandle, (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc))) {
        SIZE_T memoryUsageBytes = pmc.WorkingSetSize; // 获取当前使用的物理内存
        double memoryUsageMB = memoryUsageBytes / (1024.0 * 1024.0); // 转换为 MB
        return memoryUsageMB;
    }

    return 0.0;
}



void pauseProcess(int milliseconds) {
    QEventLoop loop;
    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
    timer.start(milliseconds);
    loop.exec(); // 进入事件循环，直到定时器超时
}



#else

// 针对非 Windows 平台的实现
double MemoryMonitor::getCurrentMemoryUsageMB() {
    // 在这里实现其他平台的内存监控逻辑，当前返回 0 作为默认值
    return 0.0;
}

#endif
